{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('buzzers.csv') as data:\n",
    "    ignore = data.readline()\n",
    "    flights = {}\n",
    "    for line in data:\n",
    "        k, v = line.strip().split(',')\n",
    "        flights[k] = v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'09:35': 'FREEPORT',\n",
       " '09:55': 'WEST END',\n",
       " '10:45': 'TREASURE CAY',\n",
       " '11:45': 'ROCK SOUND',\n",
       " '12:00': 'TREASURE CAY',\n",
       " '17:00': 'FREEPORT',\n",
       " '17:55': 'ROCK SOUND',\n",
       " '19:00': 'WEST END'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19:00\n",
      "10:45\n",
      "17:00\n",
      "09:35\n",
      "12:00\n",
      "09:55\n",
      "11:45\n",
      "17:55\n"
     ]
    }
   ],
   "source": [
    "for time in flights.keys():\n",
    "    print(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WEST END\n",
      "TREASURE CAY\n",
      "FREEPORT\n",
      "FREEPORT\n",
      "TREASURE CAY\n",
      "WEST END\n",
      "ROCK SOUND\n",
      "ROCK SOUND\n"
     ]
    }
   ],
   "source": [
    "for destination in flights.values():\n",
    "    print(destination)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['FREEPORT', 'ROCK SOUND', 'TREASURE CAY', 'WEST END']\n"
     ]
    }
   ],
   "source": [
    "print(sorted(set(flights.values())))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FREEPORT\n",
      "ROCK SOUND\n",
      "TREASURE CAY\n",
      "WEST END\n"
     ]
    }
   ],
   "source": [
    "for where_to in sorted(set(flights.values())):\n",
    "    print(where_to)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "locations = []\n",
    "for where_to in sorted(set(flights.values())):\n",
    "    locations.append(where_to)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['FREEPORT', 'ROCK SOUND', 'TREASURE CAY', 'WEST END']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "locations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "locations = [where_to for where_to in sorted(set(flights.values()))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['FREEPORT', 'ROCK SOUND', 'TREASURE CAY', 'WEST END']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "locations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def _24to12(time24: str) -> str:\n",
    "    \"\"\"Accepts a time string is 24-hour format and returns the\n",
    "       equivalent AM/PM version\"\"\"\n",
    "    return(datetime.strptime(time24, \"%H:%M\").strftime(\"%I:%M%p\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'09:00AM'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_24to12('09:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'09:00PM'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_24to12(\"21:00\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://docs.python.org/3/library/datetime.html?#strftime-and-strptime-behavior"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "times = []\n",
    "for flight_time in flights.keys():\n",
    "    times.append(flight_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['19:00', '10:45', '17:00', '09:35', '12:00', '09:55', '11:45', '17:55']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "times = []\n",
    "for flight_time in flights.keys():\n",
    "    times.append(_24to12(flight_time))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['07:00PM',\n",
       " '10:45AM',\n",
       " '05:00PM',\n",
       " '09:35AM',\n",
       " '12:00PM',\n",
       " '09:55AM',\n",
       " '11:45AM',\n",
       " '05:55PM']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "times = [_24to12(flight_time) for flight_time in flights.keys()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['07:00PM',\n",
       " '10:45AM',\n",
       " '05:00PM',\n",
       " '09:35AM',\n",
       " '12:00PM',\n",
       " '09:55AM',\n",
       " '11:45AM',\n",
       " '05:55PM']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "times = []\n",
    "for flight_time in flights.keys():\n",
    "    if flight_time < '12:00':\n",
    "        times.append(_24to12(flight_time))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(times)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "am_times = [_24to12(ft) for ft in flights.keys() if ft < '12:00']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['10:45AM', '09:35AM', '09:55AM', '11:45AM']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "am_times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(am_times)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len([_24to12(ft) for ft in flights.keys() if ft < '12:00'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len([_24to12(ft) for ft in flights.keys() if ft >= '12:00'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pm_times = [_24to12(ft) for ft in flights.keys() if ft >= '12:00']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['07:00PM', '05:00PM', '12:00PM', '05:55PM']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pm_times"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exercise from existing material."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "flights2 = { k: v for k, v in zip([_24to12(t) for t in flights.keys()], [d.title() for d in flights.values()]) }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'05:00PM': 'Freeport',\n",
       " '05:55PM': 'Rock Sound',\n",
       " '07:00PM': 'West End',\n",
       " '09:35AM': 'Freeport',\n",
       " '09:55AM': 'West End',\n",
       " '10:45AM': 'Treasure Cay',\n",
       " '11:45AM': 'Rock Sound',\n",
       " '12:00PM': 'Treasure Cay'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'09:35': 'FREEPORT',\n",
       " '09:55': 'WEST END',\n",
       " '10:45': 'TREASURE CAY',\n",
       " '11:45': 'ROCK SOUND',\n",
       " '12:00': 'TREASURE CAY',\n",
       " '17:00': 'FREEPORT',\n",
       " '17:55': 'ROCK SOUND',\n",
       " '19:00': 'WEST END'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dests2times = {}\n",
    "for time, destination in flights2.items():\n",
    "    if destination not in dests2times:\n",
    "        dests2times[destination] = []\n",
    "    dests2times[destination].append(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Freeport': ['09:35AM', '05:00PM'],\n",
       " 'Rock Sound': ['11:45AM', '05:55PM'],\n",
       " 'Treasure Cay': ['10:45AM', '12:00PM'],\n",
       " 'West End': ['09:55AM', '07:00PM']}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dests2times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dests2times_v2 = { }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
